package com.dabai.sas.activitys;

import android.Manifest;
import android.os.Build;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;

import com.dabai.sas.R;
import com.dabai.sas.utils.DabaiUtils;
import com.tbruyelle.rxpermissions3.RxPermissions;

import java.util.Objects;

public class PermissionsActivity extends AppCompatActivity {

    final RxPermissions rxPermissions = new RxPermissions(this);

    Button bt1;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_permissions);

        bt1 = findViewById(R.id.button1);
        Objects.requireNonNull(getSupportActionBar()).setDisplayHomeAsUpEnabled(true);

    }

    @RequiresApi(api = Build.VERSION_CODES.M)
    public void accessToFilePermissionsApplyButton(View view) {

        requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 101); // 动态申请读取权限

    }

    @Override
    protected void onResume() {
        super.onResume();

        checkPermissions(bt1, Manifest.permission.WRITE_EXTERNAL_STORAGE);

    }


    //菜单
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.permission_menu, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                return true;
            case R.id.open_info:
                new DabaiUtils().toMyAppInfo(this);
                break;
        }
        return super.onOptionsItemSelected(item);
    }


    public void checkPermissions(Button bt, String Man) {
        if (rxPermissions.isGranted(Man)) {
            bt.setEnabled(false);
            bt.setText("已授权");
        } else {
            bt.setEnabled(true);
            bt.setText("申请");
        }
    }


}